<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
  import echarts from 'echarts'
  require('echarts/theme/macarons') // echarts theme
  import resize from './mixins/resize'

  const animationDuration = 6000

  export default {
    mixins: [resize],
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '1800px'
      },
      height: {
        type: String,
        default: '100%'
      },
      PositionXdata: Array,
      PositionYdata: Array,
      type: Array,
      typenumber: String,
      typeXrotate: Array,
      typeXbarwidth: Array
    },
    data() {
      return {
        chart: null
      }
    },
    mounted() {
      this.$nextTick(() => {
        this.initChart()
      })
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        this.chart.setOption({
          title: {
            text: this.type[this.typenumber],
            textStyle: {
              color: 'yellow',
              fontSize: 30
            },
            x: 'center',
            y: 'top'
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: { // 坐标轴指示器，坐标轴触发有效
              type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
            }
          },
          grid: {
            top: '25%',
            left: '20%',
            containLabel: true
          },
          xAxis: [{
            type: 'category',
            data: this.PositionXdata[this.typenumber],
            textStyle: {
              color: 'white'
            },
            axisLabel: {
              interval: 0,
              rotate: this.typeXrotate[this.typenumber],
              textStyle: {
                color: '#ffad24'
              }
            },
            axisTick: {
              alignWithLabel: true
            }
          }],
          yAxis: [{
            type: 'value',
            scale : true,
            max : 10,
            min : 0,
            splitNumber : 5,
            boundaryGap : [ 0.2, 0.2 ],
            axisTick: {
              show: false
            }
          }],
          series: [{
            name: '设备部位点检统计',
            type: 'bar',
            animationDuration: 100,
            barWidth: this.typeXbarwidth[this.typenumber],
            itemStyle: {
              normal: {
                label: {
                  show: true,
                  position: 'top',
                  formatter: '{c}'
                },
                color: '#ffad24'
              }
            },
            data: this.PositionYdata[this.typenumber]
          }]
        })
      },
      reloadFun() {
        this.reload()
      }
    }
  }
</script>
